<template>
  <div class="searchbox">
    <el-input
      v-model.lazy="keyword"
      placeholder="请输入关键词搜索文章"
      style="width:250px"
    ></el-input>
    <transition name="el-fade-in-linear">
      <div v-if="show && position == 'left'" class="box u-tri">
        <div class="successbox" v-if="success">
          <h3>搜索结果</h3>
          <vue-scroll :ops="ops">
            <div class="re-box">
              <div
                v-for="(item, index) in articleArray"
                :key="index"
                @click="handlerarticle(item._id)"
                class="s-box"
              >
                <span class="s-title">{{ item.title }}</span>
                <span class="s-category">{{ item.category[0] }}</span>
              </div>
            </div>
          </vue-scroll>
        </div>
        <div v-else class="noresult">无匹配结果,请修改关键词！</div>
      </div>
      <div v-if="show && position == 'right'" class="box_right u_tri_right ">
        <div class="successbox" v-if="success">
          <h3>搜索结果</h3>
          <vue-scroll :ops="ops">
            <div class="re-box">
              <div
                v-for="(item, index) in articleArray"
                :key="index"
                @click="handlerarticle(item._id)"
                class="s-box"
              >
                <span class="s-title">{{ item.title }}</span>
                <span class="s-category">{{ item.category[0] }}</span>
              </div>
            </div>
          </vue-scroll>
        </div>
        <div v-else class="noresult">无匹配结果,请修改关键词！</div>
      </div>
    </transition>
  </div>
</template>
<script>
import request from "../../api/request"
export default {
  data() {
    return {
      ops: {
        //vuescroll的配置
        vuescroll: {
          sizeStrategy: "percent",
          detectResize: true,
        },
        scrollPanel: {
          scrollingX: false,
        },
        rail: {},
        bar: {
          showDelay: 500,
          onlyShowBarOnScroll: false,
          keepShow: true,
          background: "#b3c0d1",
          opacity: 1,
          hoverStyle: false,
          specifyBorderRadius: false,
          minSize: false,
          size: "6px",
          disable: false,
        },
      },
      keyword: "",
      show: false,
      articleArray: [],
      success: false,
      timer: null,
    }
  },
  props: ["position"],
  watch: {
    keyword(newvalue, oldvalue) {
      if (newvalue != "") {
        this.show = true
        // 防抖
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.getarticle(this.keyword)
        }, 500)
      } else {
        this.show = false
      }
    },
  },
  methods: {
    async getarticle(keyword) {
      let res = await request({
        url: "/article/search",
        data: {
          keyword,
        },
        method: "get",
      })
      this.articleArray = []
      this.articleArray.push(...res.data)
      if (this.articleArray.length) {
        this.success = true
      } else {
        this.success = false
      }
    },
    handlerarticle(id) {
      this.keyword = ""
      this.$router.push({ name: "article", query: { id } })
    },
  },
}
</script>
<style lang="scss" scoped>
@import "../../scss/common.scss";
@media (max-width: 600px) {
  .searchbox {
    position: relative;
    display: flex;
    align-items: center;
    /deep/ .el-input {
      width: 14rem !important;
    }
    .el-input /deep/ .el-input__inner {
      height: 2.1875rem !important;
    }
  }

  .box {
    position: absolute;
    display: inline-block;
    width: 97vw;
    height: 12.5rem;
    top: 3.2rem;
    left: -0.1rem;
    border-radius: 0.125rem;
    background-color: white;
    line-height: 20px;
    font-size: 14px;
    z-index: 2147483647;
    box-shadow: 0 5px 8px rgba(10, 16, 20, 0.24), 0 0 8px rgba(10, 16, 20, 0.12);
  }
  .u-tri:before {
    position: absolute;
    left: 10px;
    top: -20px;
    content: "";
    border-top: 12px solid transparent;
    border-bottom: 12px solid white;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
  }
  .noresult {
    width: 90vw;
    height: 12.5rem;
    line-height: 12.5rem;
    text-align: center;
    font-weight: 700;
    color: rgb(97, 97, 100);
  }
  h3 {
    margin: 0;
    margin-bottom: 0.625rem;
    padding: 0px 0.625rem;
  }
  .successbox {
    padding: 0.625rem 0;
    .re-box {
      height: 9rem;
      .s-box {
        margin: 0px 1.25rem;
        margin-bottom: 0.4rem;
        display: flex;
        justify-content: space-between;
        transition: 0.5s;
        padding: 0.3125rem 0.625rem;
        .s-title {
          width: 80%;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .s-category {
          width: 20%;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          text-align: right;
        }
      }
      .s-box:hover {
        cursor: pointer;
        color: white;
        background: rgb(64, 158, 255);
      }
    }
  }
}
@media (min-width: 600px) {
  .searchbox {
    position: relative;
  }

  .box {
    position: absolute;
    display: inline-block;
    width: 450px;
    height: 250px;
    top: 65px;
    left: 0px;
    border-radius: 2px;
    background-color: white;
    line-height: 20px;
    font-size: 14px;
    z-index: 2147483647;
    box-shadow: 0 5px 8px rgba(10, 16, 20, 0.24), 0 0 8px rgba(10, 16, 20, 0.12);
  }
  .u-tri:before {
    position: absolute;
    left: 10px;
    top: -20px;
    content: "";
    border-top: 12px solid transparent;
    border-bottom: 12px solid white;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
  }
  .box_right {
    position: absolute;
    display: inline-block;
    width: 450px;
    height: 250px;
    top: 65px;
    right: 0px;
    border-radius: 2px;
    background-color: white;
    line-height: 20px;
    font-size: 14px;
    z-index: 2147483647;
    box-shadow: 0 5px 8px rgba(10, 16, 20, 0.24), 0 0 8px rgba(10, 16, 20, 0.12);
  }
  .u_tri_right:before {
    position: absolute;
    right: 10px;
    top: -20px;
    content: "";
    border-top: 12px solid transparent;
    border-bottom: 12px solid white;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
  }
  .noresult {
    height: 250px;
    line-height: 250px;
    width: 450px;
    text-align: center;
    font-weight: 700;
    color: rgb(97, 97, 100);
  }
  .successbox {
    padding: 10px 20px;
    .re-box {
      height: 180px;

      .s-box {
        margin: 0px 20px;
        margin-bottom: 10px;
        display: flex;
        justify-content: space-between;
        transition: 0.5s;
        padding: 5px 10px;
        .s-title {
          width: 80%;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
      .s-box:hover {
        cursor: pointer;
        color: white;
        background: $ar_color;
      }
    }
  }
}
/deep/ .el-input__inner {
  height: 35px;
  font-size: 14px;
}
/deep/ .el-input__inner:focus {
  border-color: $ar_color;
}
</style>
